package com.qhg.saas_server.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qhg.saas_server.mapper.OrdersMapper;
import com.qhg.saas_server.pojo.Orders;
import com.qhg.saas_server.pojo.OrdersProduct;
import com.qhg.saas_server.pojo.dto.OrdersCondition;
import com.qhg.saas_server.pojo.dto.OrdersProductDto;
import com.qhg.saas_server.service.OrdersService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Objects;

@Service
@Transactional
public class OrdersServiceImpl implements OrdersService {
    @Autowired
    private OrdersMapper ordersMapper;
    @Override
    @Transactional(rollbackFor = Exception.class)
    public PageInfo<Orders> searchOrders(OrdersCondition ordersCondition) {
        System.out.println(ordersCondition.getSellType());
        String sortRule ="ASC";
        Integer rule = ordersCondition.getSortRule();
        if(!Objects.isNull(rule)){
            sortRule = rule == 1 ? "DESC" : "ASC";
        }
        PageHelper.startPage(ordersCondition.getCurrentPage(),ordersCondition.getDisplayCount(),"orderId "+sortRule);
        List<Orders> orders=ordersMapper.getOrders(ordersCondition);
        return new PageInfo<Orders>(orders);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public OrdersProductDto insert(OrdersProductDto ordersProductDto) {
        ordersMapper.insertOrders(ordersProductDto);
        int ordersId=ordersProductDto.getOrderId();
       if(ordersProductDto.getOrderType()==0){
           for(OrdersProduct ordersProduct:ordersProductDto.getOrdersProduct()){
               ordersProduct.setOp_orderId(ordersId);
               ordersMapper.insertOrdersProduct(ordersProduct);
               ordersMapper.subtractNum(ordersProduct);
               ordersMapper.selectOrdersProduct(String.valueOf(ordersId));

           }
       }
       else {
           for(OrdersProduct ordersProduct:ordersProductDto.getOrdersProduct()){
               ordersProduct.setOp_orderId(ordersId);
               ordersMapper.insertOrdersProduct(ordersProduct);
               ordersMapper.addNum(ordersProduct);
               ordersMapper.selectOrdersProduct(String.valueOf(ordersId));

           }
       }

        return ordersProductDto;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Integer delete(String orderId,String orderType) {
        OrdersProductDto ordersProductDto = new OrdersProductDto();
        ordersProductDto.setOrdersProduct(ordersMapper.selectOrdersProduct(orderId));
        System.out.println("数据是"+orderType);
        if(orderType.equals("0")){
            for(OrdersProduct ordersProduct:ordersProductDto.getOrdersProduct()){
                ordersMapper.addNum(ordersProduct);
            }
        }
        else {
            for(OrdersProduct ordersProduct:ordersProductDto.getOrdersProduct()){
                ordersMapper.subtractNum(ordersProduct);
            }
        }
        ordersMapper.deleteOrdersProduct(orderId);
        return ordersMapper.deleteOrders(orderId);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public OrdersProductDto select(String orderId) {
        OrdersProductDto ordersProductDto = new OrdersProductDto();
        ordersProductDto.setOrdersProduct(ordersMapper.selectOrdersProduct(orderId));
        return ordersProductDto;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Integer update(OrdersProductDto ordersProductDto) {
        ordersMapper.updateOrders(ordersProductDto);
        OrdersProductDto ordersProductDto1 = new OrdersProductDto();
        ordersProductDto1.setOrdersProduct(ordersMapper.selectOrdersProduct(String.valueOf(ordersProductDto.getOrderId())));
        if(ordersProductDto.getOrderType()==0){
            for (OrdersProduct ordersProduct:ordersProductDto1.getOrdersProduct()){
                ordersMapper.addNum(ordersProduct);
            }
        }
        else {
            for (OrdersProduct ordersProduct:ordersProductDto1.getOrdersProduct()){
                ordersMapper.subtractNum(ordersProduct);
            }
        }
        ordersMapper.deleteOrdersProduct(String.valueOf(ordersProductDto.getOrderId()));
        int ordersId=ordersProductDto.getOrderId();
        if(ordersProductDto.getOrderType()==0){
            for (OrdersProduct ordersProduct:ordersProductDto.getOrdersProduct()){
                ordersProduct.setOp_orderId(ordersId);
                ordersMapper.insertOrdersProduct(ordersProduct);
                ordersMapper.subtractNum(ordersProduct);
            }
        }
        else {
            for (OrdersProduct ordersProduct:ordersProductDto.getOrdersProduct()){
                ordersProduct.setOp_orderId(ordersId);
                ordersProduct.setOp_orderId(ordersProductDto.getOrderId());
                ordersMapper.insertOrdersProduct(ordersProduct);
                ordersMapper.addNum(ordersProduct);
            }
        }
        return ordersMapper.updateOrders(ordersProductDto);
    }
}
